#coding=utf-8
class colorformat:
    def __init__(self, string):
        self.string = string
        self.color = {
            "0" : "black",
            "1" : "dark_blue",
            "2" : "dark_green",
            "3" : "dark_aqua",
            "4" : "dark_red",
            "5" : "dark_purple",
            "6" : "gold",
            "7" : "gray",
            "8" : "dark_gray",
            "9" : "blue",
            "a" : "green",
            "b" : "aqua",
            "c" : "red",
            "d" : "light_purple",
            "e" : "yellow",
            "f" : "white"
        }
        self.format = {
            "r" : "default",
            "k" : "random",
            "l" : "strong",
            "m" : "delete",
            "n" : "underline",
            "o" : "italic"
        }
        self.keyword = "§"

    def GET_RIGHT(self, string, right_string, start_int=0):
        IDATA = string[start_int:]
        IIint = 0
        for i in range(len(IDATA)):
            if IDATA[i] == right_string:
                IIint = i
                break
        return IDATA[IIint + 1:]

    def GET_LEFT(self, string, left_string, start_int=0):
        IDATA = string[start_int:]
        IIint = 0
        for i in range(len(IDATA)):
            if IDATA[i] == left_string:
                IIint = i
                break
        return IDATA[:IIint]

    def Handle(self):
        IIList = list()
        IIList_Format = list()
        IIColor = str()
        IIStr = str()
        IReturn = list()

        for i in range(len(self.string)):
            if self.string[i] == self.keyword:
                IIList.append(i + 1)
                if i != 0 and len(IIList) <= 1:
                    IReturn.append((self.string[:i], None, None))

        for i in range(len(IIList)):
            if str(self.string[IIList[i]]) in self.color:
                IIColor = self.color[self.string[IIList[i]]]
            else:
                IIColor = None

            if str(self.string[IIList[i]]) in self.format:
                if self.format[self.string[IIList[i]]] != "default" and IIList_Format != None:
                    IIList_Format.append(self.format[self.string[IIList[i]]])
                else:
                    IIList_Format = list()

            IIStr = self.GET_RIGHT(self.string, self.string[IIList[i]], IIList[i])
            IIStr_ButEnd = self.GET_LEFT(IIStr, self.keyword)
            if IIStr_ButEnd == "":
                IIStr_ButEnd = IIStr
            #print(self.string[IIList[i] + 1])
            if IIList_Format == []:
                IIList_Format = None
            IReturn.append((IIStr_ButEnd, IIColor, IIList_Format))
        
        return IReturn

    def __str__(self):
        Istring = str()
        for string, color, formatstr in self.Handle():
            Istring += string
        return Istring